<template>
	<view class="">
		<button @click="getUserProfile">同步信息</button>
		<button @click="subscribe">订阅消息</button>
		<button @click="pay">立即购买</button>
		<button type="default" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">获取手机号</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello'
			}
		},
		onLoad() {

		},
		methods: {
			getUserProfile(e) {
				var that = this
				// 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
				// 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
				wx.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (res) => {
						// console.log(res.userInfo);
						var res = that.$myRequest({
							url: 'user/update',
							method: 'POST',
							data: {data: res.userInfo}
						}).then(res => {
							var res = res.data
							console.log(res);
							if (res.code == 200) {
								uni.showToast({
									title: res.msg,
								})
							} else {
								ni.showToast({
									title: res.msg,
									icon: 'none'
								})
							}
						})
					}
				})
			},
			subscribe() {
				console.log('subscribe');
				uni.requestSubscribeMessage({
					// tmplIds: [
					// 	'rrFDKHymi6T887wdYCKPZw7KTYfyKG-4vlZbfQi07bc',
					// 	'hrIOlf-_ARrsXm_k_1ROQxMdoVEVlz4Er7XaUttsn9E'
					// ],
					// 雷曼车服
					tmplIds: [
						'pEmjg_vBihqCo0LuwGyCbDWuxkH4aglmISBhxofIBQU',
						'L0kT12GmSE5DF8frn2tVzmmqEbOw56EExHybOaepoNk',
						// 'TjIKDtMkUCbAPmSfajyBqtx0_miN5Tti9X1wG_zxspM',
						'HoqVO4sSIuxlvbPUvVkzb-mjppPnrQUo2dQOuxG6irk',
					],
					success(res) {
						console.log('success', res);
					},
					fail(res) {
						uni.showToast({
							title: '订阅消息调起失败',
							icon: 'none'
						})
						console.log('fail', res);
					}
				})
			},
			pay(){
				uni.request({
					url: "http://liang.nat200.top/app/index.php?c=entry&a=wxapp&i=2&m=liang_tp_applet&do=api&s=/example/payment",
					// url: "http://dl.cy/api.php/example/payment",
					method: "POST",
					header: {
						token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJsaWFuZyIsImF1ZCI6ImNoZW4iLCJpYXQiOjE2MzE3OTg4NjAsIm5iZiI6MTYzMTc5ODg2MCwiZXhwIjoxNjMyNjYyODYwLCJkYXRhIjp7InVpZCI6MX19.5FPKrVnyYe9Ng7xjVXr-yqaFUb75R7IbIKX5YJaboHA"
					},
					success: res => {
						let wxPayment = res.data.data
						console.log(wxPayment)
						uni.requestPayment({
							...wxPayment,
							success(res) {
								console.log(res);
							},
							fail(err) {
								console.error(err)
							}
						})
					}
				})
			},
			decryptPhoneNumber (e) {
				if (e.detail.errMsg === 'getPhoneNumber:ok') {
					var res = this.$myRequest({
						url: '/user/decryptPhoneNumber',
				       	method: 'POST',
				       	data: {
				       		iv: e.detail.iv,
				       		encryptedData: e.detail.encryptedData,
				       	}
				    }).then(res=>{
						console.log(res.data);
					})
				} else {
				    uni.showToast({
						title: '拒绝授权',
						icon: 'none'
				    })
				}
			}
		},
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
